package com.pan.behavioral.command.utils;

import com.pan.behavioral.command.order.MyCommand;

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

public enum CreateBeanUtils {

    INSTANCE;

    private HashMap<String, MyCommand> objMap = new HashMap<>();
    private Properties configProperties;
    {
        configProperties = new Properties();
        try {
            configProperties.load(new FileReader("src/main/java/com/pan/behavioral/command/config.properties"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



    public MyCommand getPrintScreen(){

        return getScreenShot("PrintScreen");
    }

    private MyCommand getScreenShot(String key){
        if (objMap.containsKey(key)){
            return objMap.get(key);
        }
        try {
            Class<?> printScreenClass = Class.forName(configProperties.getProperty(key));
            MyCommand printScreen = (MyCommand) printScreenClass.getConstructor().newInstance();
            objMap.put(key,printScreen);
            return printScreen;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public MyCommand getSaveScreen(){
        return getScreenShot("SaveScreen");
    }
    public MyCommand getPrintAndSignatureScreen(){
        return getScreenShot("PrintAndSignatureScreen");
    }
}
